﻿using System.Windows.Forms;
using HighDiveEngine.Aggregates;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;

namespace HighDiveAdmin.GUI.Components
{
    public partial class EditContestList : UserControl
    {
        public event DelegateVoidContest EventLoadContestToEdit;
        public EditContestList()
        {
            InitializeComponent();
        }

        public void SetDataSource(ContestList contestHistoryList)
        {
            listBoxContests.DataSource = contestHistoryList;
        }

        /// <summary>
        /// Loads selected contest to the edit screen.
        /// </summary>
        private void listBoxContests_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Y > listBoxContests.SelectedIndex * 15 && e.Y < listBoxContests.SelectedIndex * 15 + 15)
            {
                if (EventLoadContestToEdit != null)
                    EventLoadContestToEdit((Contest)listBoxContests.Items[listBoxContests.SelectedIndex]);
            }
        }

        public void Update(ContestList contestList)
        {
            listBoxContests.DataSource = contestList;
        }

        #region TabStop
        private void EditContestList_TabStopChanged(object sender, System.EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
            if (TabStop)
                listBoxContests.Focus();
        }

        private void EditContestList_VisibleChanged(object sender, System.EventArgs e)
        {
            if (Visible)
                listBoxContests.Focus();
        } 
        #endregion

        private void listBoxContests_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (EventLoadContestToEdit != null)
                    EventLoadContestToEdit((Contest) listBoxContests.Items[listBoxContests.SelectedIndex]);
            }
        }
    }
}
